package com.iwayin.web.common;

import java.util.Properties;
import javax.mail.internet.MimeMessage;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;

import com.iwayin.core.common.Constants;

public class EmailSender {
	public static void main(String[] args) throws Exception {
		//String content = FreeMarkerManager.process("emailContent.ftl", null);
		boolean tag = EmailSender.send("16278610@qq.com", "优品会体验账号已开通", "你好",
				"text/html");
		System.out.println("aaaaaaaaaa=" + tag);
	}

	/**
	 * 发送邮件
	 * 
	 * @param receivers
	 *            收件人
	 * @param subject
	 *            标题
	 * @param mailContent
	 *            邮件内容
	 * @param mimetype
	 *            内容类型 默认为text/plain,如果要发送HTML内容,应设置为text/html
	 */
	public static boolean send(String receivers, String subject,
			String mailContent, String mimetype) {
		new Sender(receivers, subject, mailContent, mimetype).start();
		return true;
	}
	/**
	 * 多邮件发送
	 * @param receivers
	 * @param subject
	 * @param mailContent
	 * @param mimetype
	 * @return
	 */
	public static boolean sendList(String[] receivers, String subject,
			String mailContent, String mimetype) {
		for(int i = 0;i < receivers.length;i++){
			send(receivers[i],subject, mailContent, mimetype);
		}
		return true;
	}
	
	/**
	 * 发送邮件
	 * 
	 * @param receivers
	 *            收件人
	 * @param subject
	 *            标题
	 * @param mailContent
	 *            邮件内容
	 * @param mimetype
	 *            内容类型 默认为text/plain,如果要发送HTML内容,应设置为text/html
	 */
	public static boolean synchSend(String receivers, String subject,
			String mailContent, String mimetype) {
		try {
			JavaMailSenderImpl sender = new JavaMailSenderImpl();
			Properties props = new Properties();
			props.put("mail.smtps.auth", "true"); // Smtp服务器地址
			props.put("mail.smtp.starttls.enable", "true"); // 需要校验
			sender.setJavaMailProperties(props);
			sender.setProtocol("smtps");
			sender.setPort(465);
			sender.setHost(Constants.SYSTEM_EMAIL_HOST);
			sender.setUsername(Constants.SYSTEM_EMAIL_USERNAME);
			sender.setPassword(Constants.SYSTEM_EMAIL_PASSWORD);
			MimeMessage mimeMessage = sender.createMimeMessage();
			MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,
					"utf-8");
			helper.setTo(receivers);
			helper.setFrom(Constants.SYSTEM_EMAIL_USERNAME,Constants.SYSTEM_EMAIL_SHOW_NAME);
			helper.setSubject(subject);
			helper.setText(mailContent, true);
			sender.send(mimeMessage);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

}

/**
 * 线程类
 */
class Sender extends Thread {
	protected Log loger = LogFactory.getLog(this.getClass());
	private String receivers;
	private String subject;
	private String mailContent;
	private String mimetype;

	public Sender(String receivers, String subject, String mailContent,
			String mimetype) {
		this.mailContent = mailContent;
		this.mimetype = mimetype;
		this.receivers = receivers;
		this.subject = subject;
	}

	@Override
	public void run() {
		try {
			JavaMailSenderImpl sender = new JavaMailSenderImpl();
			Properties props = new Properties();
			props.put("mail.smtps.auth", "true"); // Smtp服务器地址
			props.put("mail.smtp.starttls.enable", "true"); // 需要校验
			sender.setJavaMailProperties(props);
			sender.setProtocol("smtps");
			sender.setPort(465);
			sender.setHost(Constants.SYSTEM_EMAIL_HOST);
			sender.setUsername(Constants.SYSTEM_EMAIL_USERNAME);
			sender.setPassword(Constants.SYSTEM_EMAIL_PASSWORD);
			MimeMessage mimeMessage = sender.createMimeMessage();
			MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,
					"utf-8");
			helper.setTo(receivers);
			helper.setFrom(Constants.SYSTEM_EMAIL_USERNAME,Constants.SYSTEM_EMAIL_SHOW_NAME);
			helper.setSubject(subject);
			helper.setText(mailContent, true);
			sender.send(mimeMessage);
		} catch (Exception e) {
			loger.error("send email fail", e);
		}
	}
}
